package com.pinjitsu.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.pinjitsu.service.HighscoreService;
import com.pinjitsu.service.RestClientService;

@Controller
// @Scope("session")
public class MainController {

    HighscoreService highscoreService;
    RestClientService restClientService;
    private String appId;
    private String canvasPage;

    @RequestMapping( "/" )
    public ModelAndView get(HttpServletRequest request) {

        // TODO - fix
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", System.currentTimeMillis());

        // if (StringUtils.isEmpty(code)) {
        // /*modelAndView.setViewName("redirect:http://www.facebook.com/dialog/oauth?client_id=" + appId
        // + "&redirect_uri=" + request.getRequestURL().toString().replace("app/", "")
        // "http://apps.facebook.com/pinjitsu/" + "&scope=email,read_stream");*/
        // // return modelAndView;
        // String authUrl = "http://www.facebook.com/dialog/oauth?client_id=" + appId
        // + "&redirect_uri=" + canvasPage + "&scope=email,read_stream";
        // modelAndView.addObject("authUrl", authUrl);
        // modelAndView.setViewName("redirect");
        // return modelAndView;
        // }
        //
        // String accessTokenRsp = restClientService.getAccessToken(canvasPage, code);

        // modelAndView.addObject("count", getHighscoreService().countHighscores());
        // FacebookXmlRestClient client = (FacebookXmlRestClient) request.getSession().getAttribute(
        // FacebookInterceptor.FACEBOOK_USER_CLIENT);
        // modelAndView.addObject("accessToken", accessTokenRsp.substring("access_token=".length()));
        // modelAndView.addObject("restClientService", restClientService);
        // modelAndView.addObject("appId", appId);
        // modelAndView.addObject("facebookClient", client);

        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping( "/friends/list" )
    @ResponseBody
    public String getFriends(@RequestParam( "access_token" ) String accessToken) {
        return restClientService.getFriends(accessToken);
    }

    public HighscoreService getHighscoreService() {
        return highscoreService;
    }

    @Autowired
    public void setHighscoreService(HighscoreService highscoreService) {
        this.highscoreService = highscoreService;
    }

    public RestClientService getRestClientService() {
        return restClientService;
    }

    @Autowired
    public void setRestClientService(RestClientService restClientService) {
        this.restClientService = restClientService;
    }

    public String getAppId() {
        return appId;
    }

    @Value( "${facebook.appId}" )
    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getCanvasPage() {
        return canvasPage;
    }

    @Value( "${facebook.canvasPage}" )
    public void setCanvasPage(String canvasPage) {
        this.canvasPage = canvasPage;
    }

}
